package config

import (
	"fmt"
	"log"
	"os"

	"gopkg.in/yaml.v3"
)

type Mysql struct {
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Database string `yaml:"database"`
	Charset  string `yaml:"charset"`
	Timeout  string `yaml:"timeout"`
}

type Redis struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`
}

var (
	DatabaseConfig = &Mysql{}
	RedisConfig    = &Redis{}
)

func Setup() {
	var config struct {
		Database *Mysql `yaml:"database"`
		Redis    *Redis `yaml:"redis"`
	}
	configPath := "app/yaml/config.yaml"

	var data []byte
	var err error

	data, err = os.ReadFile(configPath)
	if err != nil {
		fmt.Printf("Error reading config file: %v", err)
		log.Fatalf("Error reading config file: %v", err)
	}

	if err := yaml.Unmarshal(data, &config); err != nil {
		log.Fatalf("setting.Setup: failed to decode config data: %v", err)
	}

	DatabaseConfig = config.Database
	RedisConfig = config.Redis
}
